core.form = {};

core.form.UIControl = function(aParams){
	this.init(aParams);
}

core.extendClass('core.form.UIControl', 'core.Box');

core.form.UIControl.prototype.init = function(aParams){
	this.superCall('init', arguments, aParams);
}

core.form.UIControl.prototype.focus = function(){
	this.fire('focus');
}

core.form.UIControl.prototype.unfocus = function(){
	this.fire('unfocus');
}

core.form.UIControl.prototype.doFocus = function(){
	return true;
}

core.form.UIControl.prototype.setDisabled = function(aDisabled){
	this.disabled = aDisabled;
	return true;
}

core.form.UIControl.prototype.doUnFocus = function(){
	
}

core.form.UIControl.prototype.doKeyPress = function(){
	return true;
}

core.Panel = function(aParams){
	this.init(aParams);
}

core.extendClass('core.Panel', 'core.Box');

core.Panel.prototype.init = function(aParams){
	var instance = this;
	this.scrollableMode = aParams.scrollable;
	if(this.scrollableMode){
		this.minWidth = 10;
		this.minHeight = 10;
	}
	this.controls = [];
	this.focused = false;
	this.on('childAdded', function(i, e, child){
		var setUpHandlers = function(child){
			instance.controls.push(child);
			child.on('focus', function(i){
				if(instance.currentControl){
					instance.currentControl.doUnFocus();
				}
				instance.currentControl = i;
				i.doFocus();
				instance.focused = true;
//                core.log('Focused panel:', instance.instanceID);
                desktop.setFocusedPanel(instance);
			});
		}
		
		if(child.instanceOf('core.form.UIControl')){
			setUpHandlers(child);
		}
		child.collectChildren('core.form.UIControl', setUpHandlers);
	});
	this.superCall('init', arguments, aParams);
}

core.Panel.prototype.unfocus = function(){
    if(this.currentControl){
        this.currentControl.doUnFocus();
    }
    this.focused = false;
}

core.Panel.prototype.setWidth = function(aWidth){
	this.superCall('setWidth', arguments, aWidth);
	if(this.scrollableMode)
		this.width = aWidth;
}

core.Panel.prototype.getMinWidth = function(){
	var minWidth = this.superCall('getMinWidth', arguments);
	if(this.scrollableMode){
		this.bodyMinWidth = minWidth;
		this.minWidth = 10;
	}
	return this.minWidth;
}

core.Panel.prototype.setHeight = function(aHeight){
	this.superCall('setHeight', arguments, aHeight);
	if(this.scrollableMode)
		this.height = aHeight;
}

core.Panel.prototype.getMinHeight = function(){
	var minHeight = this.superCall('getMinHeight', arguments);
	if(this.scrollableMode){
		this.bodyMinHeight = minHeight;
		this.minHeight = 10;
	}
	return this.minHeight;
}

core.Panel.prototype.resize = function(aX, aY, aWidth, aHeight){
//	core.log('panel resize', aWidth, this.minWidth, this.width, $(this.div).scrollTop());
    var scrollTop = $(this.div).scrollTop();
    var scrollLeft = $(this.div).scrollLeft();
	if(this.scrollableMode)
		$(this.div).removeClass('scrollable');
	this.superCall('resize', arguments, aX, aY, aWidth, aHeight);
	
	if(this.scrollableMode)
		$(this.div).addClass('scrollable');
    $(this.div).scrollTop(scrollTop).scrollLeft(scrollLeft);
//	$(this.div).width(aWidth);
//	$(this.div).height(aHeight);
}

core.Panel.prototype.processKeyDown = function(e){
	if(!this.focused || desktop.focusedPanel!=this)
		return true;
	if(!this.superCall('processKeyDown', arguments, e))
		return false;
	if(e.which==9 && this.focused && !e.ctrlKey){
		if(this.controls.length==0)
			return true;
		var index = 0;
		for(i = 0; i<this.controls.length; i++){
			if(this.controls[i]==this.currentControl){
				index = i;
				break;
			}
		}
		var direction = e.shiftKey?-1:1;
		var newIndex = index;
		newIndex += direction;
		if(newIndex<0)
			newIndex = this.controls.length-1;
		if(newIndex>=this.controls.length)
			newIndex = 0;
		this.currentControl.doUnFocus();	
		this.currentControl = this.controls[newIndex];
		this.currentControl.doFocus();
		return false;
	}else{
		if(this.currentControl)
			return this.currentControl.doKeyPress(e);
	}
	return true;
}

core.form.TextField = function(aParams){
	this.init(aParams);
}

core.extendClass('core.form.TextField', 'core.form.UIControl');

core.form.TextField.prototype.init = function(aParams){
	this.superCall('init', arguments, aParams);
	
	this.control = document.createElement('input');
	this.control.type = 'text';
	$(this.control).addClass('input_field');
	this.div.appendChild(this.control);
	this.minHeight = 24;
	this.minWidth = 224;
	var inst = this;
	$(this.control).blur(function(e){
		return inst.fire('unfocus', e);
	});
	$(this.control).focus(function(e){
		return inst.fire('focus', e);
	});
	$(this.control).keydown(function(e){
		return inst.fire('keydown', e);
	});
}

core.form.TextField.prototype.doFocus = function(){
	this.control.focus();
	return false;
}

core.form.TextField.prototype.doUnFocus = function(){
	if(!jQuery.browser.msie)
		this.control.blur();
	return true;
}


core.form.TextField.prototype.resize = function(aX, aY, aWidth, aHeight){
	this.superCall('resize', arguments, aX, aY, aWidth, aHeight);
	$(this.control).width($(this.div).innerWidth());
	this.alignBlock(this.control, $(this.div).innerWidth(), $(this.div).innerHeight());
}

core.form.TextArea = function(aParams){
	this.init(aParams);
}

core.extendClass('core.form.TextArea', 'core.form.UIControl');

core.form.TextArea.prototype.init = function(aParams){
	this.superCall('init', arguments, aParams);
	
	this.control = document.createElement('textarea');
	$(this.control).addClass('input_area');
	this.div.appendChild(this.control);
	this.minHeight = 20;
	this.minWidth = 25;
	var inst = this;
	
	$(this.control).blur(function(e){
		inst.fire('unfocus', e);
	});
	$(this.control).focus(function(e){
		inst.fire('focus', e);
	});
	$(this.control).keydown(function(e){
		return inst.fire('keydown', e);
	});
}

core.form.TextArea.prototype.doFocus = function(){
	this.control.focus();
	return false;
}

core.form.TextArea.prototype.doUnFocus = function(){
	if(!jQuery.browser.msie)
		this.control.blur();
	return true;
}


core.form.TextArea.prototype.resize = function(aX, aY, aWidth, aHeight){
	this.superCall('resize', arguments, aX, aY, aWidth, aHeight);
	$(this.control).width($(this.div).innerWidth());
	$(this.control).height($(this.div).innerHeight()-2);
//	this.alignBlock(this.control, $(this.div).innerWidth(), $(this.div).innerHeight());
}
